/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JavaClass;

import DAO.LessonListDAO;
import generate.lessonList.LessonLists;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author dienvm60347
 */
public class LessonListSAXProcessor implements Serializable{

    public LessonListSAXProcessor() {
    }
    public List save (String filename, String optID){
        try {
            LessonListSAXParser objSAX = new LessonListSAXParser(optID);
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parse = spf.newSAXParser();

            File file = new File(filename);
            if(!file.exists()){
                LessonListDAO lesson = new LessonListDAO();
                LessonLists list = new LessonLists(lesson.getAllLesson());
                JaxBFunction.marshall(filename, list);
            }
            parse.parse(file, objSAX);
            return objSAX.getLessonlist();

        } catch (IOException ex) {
            Logger.getLogger(LessonListSAXProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(LessonListSAXProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(LessonListSAXProcessor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
